<template>
  <el-select style="width:100%" filterable v-model="model" v-bind="$attrs" clearable v-on="$listeners">
    <template v-for="(item) of options">
      <el-option :key="item.userId" :label="item.nickName" :value="item.userId"></el-option>
    </template>
  </el-select>
</template>

<script>
import { listUser } from '@/api/system/user'
export default {
  model: {
    prop: "value",
    event: "change"
  },
  data() {
    return {
      options: []
    }
  },
  props: {
    value: {
      type: [String, Number,Array],
      default: undefined
    },
    deptId: {
      type: [String, Number],
      required: false
    },
    defaultSelect:{
      type:Boolean,
      default:false
    }
  },
  computed: {
    model: {
      get() {
        return this.value?this.value : void 0
      },
      set(value) {
        this.$emit("change", value)
      }
    }
  },
  watch: {
    deptId() {
      this.getUsers();
    }
  },
  mounted() {
    this.getUsers();
  },
  methods: {
    /**
     * 去掉值
     */
    clear(){
      this.$emit("change",void 0)
    },
    /**
     * 获取类型
     */
    getUsers() {
      if(!this.deptId){
        this.options = [];
        return;
      }
      let params = {
        deptId: this.deptId,
        status: 0,
        delFlag:0,
        pageSize:9999999,
      }
      return new Promise((resolve,reject)=>{
        listUser(params).then(res => {
          res.rows.forEach(element => {
            element.userId = String(element.userId)
          });
          this.options = res.rows;
          resolve();
        }).catch(error => {
          this.options = []
          resolve();
        })
      })
    }
  }
}
</script>

<style>
</style>